package cn.com.dhc.danlu.widget.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.com.dhc.danlu.R;
import cn.com.hd.mall.web.webservices.entity.response.mydl.invoice.InvoiceListResponse.InvoiceListModel;

public class NormalInvoiceAdapter extends BaseAdapter {
    /**
     * 默认发票为0
     */
    private static final String DEFAULT_NORMAL_INVOICE = "0";
    private Context context;
    /**
     * 接收普通发票类
     */
    private List<InvoiceListModel> normalInvoiceList;
    private int selectedIndex = 0;

    public NormalInvoiceAdapter(Context context, List<InvoiceListModel> normalInvoiceList) {
        this.context = context;
        this.normalInvoiceList = normalInvoiceList;
    }

    @Override
    public int getCount() {
        return normalInvoiceList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return normalInvoiceList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        NormalInvoiceHolder holder;
        if (arg1 == null) {
            holder = new NormalInvoiceHolder();
            arg1 = LayoutInflater.from(context).inflate(R.layout.layout_normal_invoice_item, null);
            holder.norIvCP = (TextView) arg1.findViewById(R.id.norIvCP);
            holder.norIvDefault = (TextView) arg1.findViewById(R.id.norIvDefault);
            holder.norIvImg = (ImageView) arg1.findViewById(R.id.norIvImg);
            arg1.setTag(holder);
        } else {
            holder = (NormalInvoiceHolder) arg1.getTag();
        }
        setData(holder, arg0);
        if (selectedIndex == arg0) {
            // 选中
            holder.norIvImg.setImageResource(R.drawable.ic_check);
        } else {
            // 未选中
            holder.norIvImg.setImageResource(R.drawable.ic_not_check);
        }
        return arg1;
    }

    private void setData(NormalInvoiceHolder holder, int position) {
        if (DEFAULT_NORMAL_INVOICE.equals(normalInvoiceList.get(position).getIsDefault())) {
            holder.norIvDefault.setVisibility(View.VISIBLE);
            holder.norIvDefault.getPaint().setFakeBoldText(true);

            if (normalInvoiceList.get(position).getInvoiceHeader().length() > 20) {
                String noramalInvoiceName = normalInvoiceList.get(position).getInvoiceHeader();
                noramalInvoiceName = noramalInvoiceName.substring(0, 20);
                noramalInvoiceName = noramalInvoiceName + "...";
                holder.norIvCP.setText(noramalInvoiceName);
            } else {
                holder.norIvCP.setText(normalInvoiceList.get(position).getInvoiceHeader());
            }
        } else {
            holder.norIvDefault.setVisibility(View.GONE);
            holder.norIvCP.setText(normalInvoiceList.get(position).getInvoiceHeader());
        }
    }

    public void setSelectedIndex(int index) {
        this.selectedIndex = index;
    }

    private static class NormalInvoiceHolder {
        private TextView norIvCP;
        private TextView norIvDefault;
        private ImageView norIvImg;
    }
}